package com.rxyb.security.utils;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.json.JSONUtil;
import com.rxyb.security.db.bean.SysUsercustom;
import com.rxyb.security.db.po.custom.MenuRole;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author YJH
 * @version 1.0
 * @description 权限操作
 * @date 2020/7/10 9:41
 */
@Component
public class RoleUtils {


    /**
     * 获取左侧菜单栏  默认选中页面
     *
     * @param menuRoles
     * @param view
     * @return
     */
    public List<Tree<String>> menu(List<MenuRole> menuRoles, String view) {
        // 获取等于一级菜单
        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        List<TreeNodeExtend<MenuRole>> nodeList = menuRoles.stream().map
                (e -> new TreeNodeExtend<MenuRole>(
                        e.getId().toString(),
                        e.getPid().toString(),
                        e.getMenuName(),
                        null, e.getMenuDesc(),
                        e.getMenuUrl(),
                        e.getMenuIcon(),
                        e.getMenuUrl().equals(view) ? true : false))
                .collect(Collectors.toList());
        // 最大递归深度
        treeNodeConfig.setDeep(3);
        // 获取所有子节点的选中的信息
        List<TreeNodeExtend<MenuRole>> collect = nodeList.stream().filter(e -> e.isActive() && !Integer.valueOf(e.getParentId().toString()).equals(0)).collect(Collectors.toList());

        nodeList.forEach(e -> {
            if (Integer.valueOf(e.getParentId().toString()).equals(0)) {
                Optional<TreeNodeExtend<MenuRole>> first = collect.stream().filter(t -> t.getParentId().equals(e.getParentId())).findFirst();
                if (first.isPresent()) {
                    e.setActive(true);
                }
            }
        });

        List<Tree<String>> treeNodes = TreeUtil.build(nodeList, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId().toString());
                    tree.setParentId(treeNode.getParentId().toString());
                    tree.setWeight(treeNode.getWeight());
                    tree.setName(treeNode.getName());
                    tree.putExtra("menuDesc", treeNode.getMenuDesc());
                    tree.putExtra("menuUrl",treeNode.getMenuUrl());
                    tree.putExtra("menuIcon", treeNode.getMenuIcon());
                    tree.putExtra("active", treeNode.isActive());
                });
        return treeNodes;
    }


    /**
     * 根据菜单权限获取按钮功能
     *
     * @param usercustom 菜单按钮
     * @return
     */
    public List<MenuRole> getMenuPre(SysUsercustom usercustom, String servletPath) {
        Optional<MenuRole> first = usercustom.getSysRoleList()
                .stream()
                .filter(e -> e.getMenuUrl().equals(servletPath.replace("/", ""))).findFirst();
        if (first.isPresent()) {
            return usercustom.getSysRoleList().stream().filter(e -> e.getPid().equals(first.get().getId()) && e.getMenuAlias().indexOf("ROLE_PRE") >= 0).collect(Collectors.toList());
        }
        return null;
    }
}
